package db;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;

public class DbFactory {

    private static final String CONECTION = "jdbc:mysql://localhost/sofyblogs";

    private static MysqlDataSource dataSource = null;

    private static UserDao userDao = null;

    private static DataSource getDataSource() {
        if (dataSource == null) {
            dataSource = new MysqlDataSource();
            dataSource.setUrl(CONECTION);
            dataSource.setUser("root");
            dataSource.setPassword("123");
        }
        return dataSource;
    }

    public static UserDao getUserDao() {
        if (userDao == null) {
            QueryRunner runner = new QueryRunner(getDataSource());
            userDao = new UserDao(runner);
        }
        return userDao;
    }
}
